// pages/changeDoctor/changeDoctor.js
Page({
  data: {
    doctorInfo: { //医生信息
      doctorName: '',
      doctorId: '',
      doctorPosition: '',
      doctorPay: null,
      doctorMaxPatient: null,
      doctorStartTime: '',
      doctorEndTime: '',
      doctorExpertise: '',
      doctorPic: [],
      userDoctorAdmin: '',
      userDoctorName: '',
      userDoctorPwd: '',
      doctorImage: '',
    },
    admin:''
  },
  onLoad: function (options) {
    //进来页面是带上自己的admin
    console.log("我的医生账号",options.admin)
    this.setData({
      admin:options.admin
    })
    //从数据库拿数据
    wx.cloud.callFunction({
      name:"doctorInfo",
      data:{
        action:"findOne",
        userDoctorAdmin:options.admin
      }
    }).then(res=>{
      console.log('拿数据库的数据成功',res)
      //保存自己的医师信息到本地以显示出来
      this.setData({
        doctorInfo:res.result.data[0],
      })
    }).catch(res=>{
      console.log('拿数据库的数据失败',res)
    })
  },

  //获取输入的值
  //医师姓名：
  getdoctorName(e) {
    var doctorName = "doctorInfo.doctorName"
    console.log(e.detail.value)

    this.setData({
      [doctorName]: e.detail.value
    })

  },
  //医师id：
  getdoctorId(e) {
    var doctorId = "doctorInfo.doctorId"
    console.log(e.detail.value)

    this.setData({
      [doctorId]: e.detail.value
    })

  },
  //医师职位：
  getdoctorPosition(e) {
    var doctorPosition = "doctorInfo.doctorPosition"
    console.log(e.detail.value)

    this.setData({
      [doctorPosition]: e.detail.value
    })

  },
  //预约费用：
  getdoctorPay(e) {
    var doctorPay = "doctorInfo.doctorPay"
    console.log(e.detail.value)
    this.setData({
      [doctorPay]: e.detail.value
    })
  },
  //可约人数：
  getdoctorMaxPatient(e) {
    var doctorMaxPatient = "doctorInfo.doctorMaxPatient"
    console.log(e.detail.value)

    this.setData({
      [doctorMaxPatient]: e.detail.value
    })

  },
  //【开始时间】
  getdoctorStartTime(e) {
    var doctorStartTime = "doctorInfo.doctorStartTime"
    console.log(e.detail.value)

    this.setData({
      [doctorStartTime]: e.detail.value
    })

  },
  //【结束时间】
  getdoctorEndTime(e) {
    var doctorEndTime = "doctorInfo.doctorEndTime"
    console.log(e.detail.value)

    this.setData({
      [doctorEndTime]: e.detail.value
    })

  },
  //医师专长：
  getdoctorExpertise(e) {
    var doctorExpertise = "doctorInfo.doctorExpertise"
    console.log(e.detail.value)

    this.setData({
      [doctorExpertise]: e.detail.value.replace(/\s+/g, '')
    })

  },

   //添加医师本地照片(0选择1云存储2本地）
   addDoctorPic() {
    let that = this
    //0选择图片
    wx.chooseImage({
      success(res) {
        console.log("0选择图片成功，选择的图片的信息", res)
        const ImageUrl = res.tempFilePaths[0] //图片临时地址
        let suffix = /\.[^\.]+$/.exec(ImageUrl)[0]; // 正则表达式，获取图片扩展名
        console.log('0选择的图片的临时url', ImageUrl)
        console.log('0选择的图片文件扩展名', suffix)
        //1把这张图传到云存储
        wx.cloud.uploadFile({
            count: 1,
            cloudPath: 'doctorPic/' + new Date().getTime() + suffix,
            filePath: ImageUrl
          },
          //1上传中提示
          wx.showLoading({
            title: '图片上传中...',
          })
        ).then(res => {
          console.log('1上传图片到云存储成功，图片的id是', res.fileID)
          //2把这张图片云存储id传到本地
          var i = that.data.doctorInfo.doctorPic.length //添加图片之前的图片数量
          var doctorPic = 'doctorInfo.doctorPic[' + i + ']'
          that.setData({
            [doctorPic]: res.fileID,
          })
          console.log('2添加图片后的本地诊所信息', that.data.doctorInfo)
          setTimeout(() => {
            wx.hideLoading({
              success: (res) => {
                wx.showToast({
                  title: '上传图片成功',
                })
              },
            })
          }, 1000);
        }).catch(error => {
          console.log('1上传图片到云存储失败', error)
          wx.hideLoading({
            success: (res) => {
              wx.showToast({
                icon: 'none',
                title: '上传图片失败',
              })
            },
          })
        })
      }
    })
  },
  //删除医生本地照片((00选择11云存储22本地）
  delDoctorPic: function (e) {
    console.log('picid', e.currentTarget.dataset.picid)
    var that = this
    //00删除确认
    wx.showModal({
      title: '提示',
      content: '确定要删除这张图片吗',
      success(res) {
        if (res.confirm) {
          console.log('00用户点击确定')
          //00删除中提示
          wx.showLoading({
            title: '正在删除...',
          })
          //11删除云存储文件
          wx.cloud.deleteFile({
            fileList: [e.currentTarget.dataset.picid]
          }).then(res => {
            console.log('11删除云文件成功', res.fileList)
            //22删除本地图片
            var doctorPic = 'doctorInfo.doctorPic'
            for (var i = 0; i < that.data.doctorInfo.doctorPic.length; i++) { //循环现有本地图片数组
              if (that.data.doctorInfo.doctorPic[i] === e.currentTarget.dataset.picid) { //比对本地图片数组中和所选图片一致的下标
                that.data.doctorInfo.doctorPic.splice(i, 1) //利用splice在本地删除对应下标的图片，是的原数组改变为新数组！
                that.setData({
                  [doctorPic]: that.data.doctorInfo.doctorPic
                })
              }
            }
            console.log('22删除后本地图片数组', that.data.doctorInfo.doctorPic)
            wx.hideLoading({
              success: (res) => {
                wx.showToast({
                  title: '删除成功',
                })
              },
            })

          }).catch(error => {
            console.log('11删除云文件失败', error)
            wx.hideLoading({
              success: (res) => {
                wx.showToast({
                  icon: 'none',
                  title: '删除图片失败',
                })
              },
            })
          })
        } else if (res.cancel) {
          console.log('00用户点击取消')
        }
      }
    })
  },

  //提交
  commit(){
    var that = this
    wx.showLoading({
      title: '医师更改中...',
    })
    if (!this.data.doctorInfo.doctorName) {
      wx.showToast({
        icon: 'none',
        title: '医师姓名不能为空',
      })
      return
    }
    if (!this.data.doctorInfo.doctorId) {
      wx.showToast({
        icon: 'none',
        title: '医师id不能为空',
      })
      return
    }
    
    if (!this.data.doctorInfo.doctorPosition) {
      wx.showToast({
        icon: 'none',
        title: '医师职位不能为空',
      })
      return
    }
    if (!this.data.doctorInfo.doctorPay) {
      wx.showToast({
        icon: 'none',
        title: '预约费用不能为空',
      })
      return
    }
    if (!this.data.doctorInfo.doctorMaxPatient) {
      wx.showToast({
        icon: 'none',
        title: '可约人数不能为空',
      })
      return
    }
    if (!this.data.doctorInfo.doctorStartTime) {
      wx.showToast({
        icon: 'none',
        title: '开始时间不能为空',
      })
      return
    }
    if (!this.data.doctorInfo.doctorEndTime) {
      wx.showToast({
        icon: 'none',
        title: '结束时间不能为空',
      })
      return
    }
    if (!this.data.doctorInfo.doctorExpertise) {
      wx.showToast({
        icon: 'none',
        title: '医师专长不能为空',
      })
      return
    }
    if (this.data.doctorInfo.doctorPic.length == 0) {
      wx.showToast({
        icon: 'none',
        title: '医师图片不能为空',
      })
      return
    }
    wx.cloud.callFunction({
      name: "doctorInfo",
      data: {
        action: "change",
        userDoctorAdmin:that.data.admin,
        doctorName: that.data.doctorInfo.doctorName,
        doctorId: that.data.doctorInfo.doctorId,
        doctorPosition: that.data.doctorInfo.doctorPosition,
        doctorPay: parseFloat(that.data.doctorInfo.doctorPay),
        doctorMaxPatient: parseInt(that.data.doctorInfo.doctorMaxPatient, 10),
        doctorStartTime: that.data.doctorInfo.doctorStartTime,
        doctorEndTime: that.data.doctorInfo.doctorEndTime,
        doctorExpertise: that.data.doctorInfo.doctorExpertise,
        doctorPic: that.data.doctorInfo.doctorPic,
      }
    }).then(res => {
      console.log('21修改医师信息成功', res)
      wx.hideLoading({
        success: (res) => {
          wx.showToast({
            title: '更改医师成功',
          })
          setTimeout(function () {
            wx.navigateBack({})
          }, 1000)
        },
      })
    }).catch(res => {
      console.log('21医师信息更改失败', res)
      wx.hideLoading({
        success: (res) => {
          wx.showToast({
            icon: 'none',
            title: '更改医师失败',
          })
        },
      })
    })
  }
})